SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO




CREATE PROC  [dbo].[SP_TXMVTO_CNTB_Q05]
/*-------------------------------------------------------------------*/
/*----- Empresa           : OFISIS S.A.                          ----*/
/*----- Cliente           : OFISIS S.A.                          ----*/
/*----- Sistema           : Contabilidad                         ----*/
/*----- Modulo            : Empresa                              ----*/
/*----- Programa          : Reporte de Ajuste por Inflacion      ----*/
/*----- Script                :                                                      -------*/
/*----- Nombre   SP      : SP_TXMVTO_CNTB_Q05                   ----*/ 
/*----- Desarrollado por  : Betsy Sanchez Arenas                 ----*/
/*----- Fecha  Creaci?n              : 13/10/1997                           ----*/
/*----- Base de Datos     : MS SQL Server                        ----*/
/*----- Version BD        : 8.0                                  ----*/
/*----- Invoca a SP      :                                        -------*/
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/*----- Modificado 1 por  : Martin Garcia Farfan                 ----*/
/*----- Fecha Modifica    : 18/03/1999                           ----*/
/*----- Detalle Modifica  : Estandarizacion                      ----*/
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/*----- Modificado 2 por  : Martin Garcia Farfan                 ----*/
/*----- Fecha Modifica    : 26/03/1999                           ----*/
/*----- Detalle Modifica  : Si es el mes 0 toma el factor del    ----*/
/*-----                     ao pasado                           ----*/
/*-------------------------------------------------------------------*/
/*----- Modificado 3 por  : Marco Acua Paredes                 ----*/
/*----- Fecha Modifica    : 03/10/2002                           ----*/
/*----- Detalle Modifica  : Nueva Versi?n    ----*/

/*-------------------------------------------------------------------*/
/*----- Comentarios       : Reporte de Movimientos por Voucher   ----*/
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/*----- Drop proc SP_TXMVTO_CNTB_Q05                             ----*/
/*----- Grant all on SP_TXMVTO_CNTB_Q05 to PUBLIC                ----*/
/*----- SP_TXMVTO_CNTB_Q05                                       ----*/
/*-------------------------------------------------------------------*/
@ISCO_EMPR           TD_VC_002,
@INNU_CNTB_EMPR      TD_IN_001_CONT,
@INNU_ANNO           TD_IN_004_ANNO,
@INNU_MESE           TD_IN_002_MESE,
@ISCO_CNTA           TD_VC_020_CNTA,
@ISTI_SALD           TD_VC_001,
@INNV_INFO_CNTA      TD_IN_001
as
Create Table #TWMVTO_CNTB_Q05_1
(NU_PERI      Char(7)       null,
 DE_GLOS      varchar(80)   null,
 HT_CARG      Numeric(16,4)         null,
 HT_ABON      Numeric(16,4)         null,
 FA_ACTU      Numeric(9,4)         null,
 AT_CARG      Numeric(16,4)        null,
 AT_ABON      Numeric(16,4)         null,
 VA_REI       Numeric(16,4)         null )
Declare 
 @CNNU_ANNO        TD_IN_004_ANNO,
 @CNNU_MESE        TD_IN_002_MESE,
 @VSNU_PERI        Char(7), 
 @CSDE_GLOS        TD_VC_100, 
 @VNHT_CARG        TD_NU_016_004,  
 @VNHT_ABON        TD_NU_016_004,  
 @VNFA_ACTU        TD_NU_009_004,
 @VNAT_CARG        TD_NU_016_004,
 @VNAT_ABON        TD_NU_016_004,    
 @CNIM_CARG        TD_NU_016_004,
 @CNIM_ABON        TD_NU_016_004,
 @VNVA_REIN        TD_NU_016_004,
 @VNNU_IND1        TD_NU_009_004,
 @VNNU_IND2        TD_NU_009_004
 
/*--- inicializo variables ---*/
Select @CNIM_CARG = 0.00,
       @CNIM_ABON = 0.00,
       @VNHT_CARG = 0,
       @VNHT_ABON = 0,
       @VNFA_ACTU = 0,
       @VNAT_CARG = 0,
       @VNAT_ABON = 0,
       @VNVA_REIN  = 0,
       @VNNU_IND1  = 0.00,
       @VNNU_IND2  = 0.00
  
/*--- obtengo indice de actualizacion a mes actual ---*/
Select @VNNU_IND2 = IN_ACTU
 From   TTINDI_ACTU  
  Where  NU_ANNO = @INNU_ANNO
  And    NU_MESE = @INNU_MESE
If @VNNU_IND2 IS null or @VNNU_IND2 = 0.00
 Select @VNNU_IND2 = 1.00
Declare CU_TXMVTO_CNTB_Q05_1 Cursor
 for
  Select NU_ANNO, NU_MESE, DE_GLOS,
  ((ASCII(TI_OPER)/67)* IM_MVTO_CNTB) , 
  ((65/ASCII(TI_OPER))* IM_MVTO_CNTB) 
  From TXMVTO_CNTB
  Where  CO_EMPR  = @ISCO_EMPR
  And  NU_CNTB_EMPR = @INNU_CNTB_EMPR
  And  NU_ANNO       = @INNU_ANNO
  And  NU_MESE       <= @INNU_MESE
  And  CO_CNTA_EMPR = Rtrim(@ISCO_CNTA)
  And  SI_MVTO_CNTB = 'APR'
  Order By CO_CNTA_EMPR, NU_ANNO, NU_MESE
  
 Open CU_TXMVTO_CNTB_Q05_1
 Fetch CU_TXMVTO_CNTB_Q05_1 Into @CNNU_ANNO, @CNNU_MESE, @CSDE_GLOS,  @CNIM_CARG, @CNIM_ABON
 While @@Fetch_status = 0
  Begin
   if @CNNU_MESE = 0
     Select @VNNU_IND1 = isnull(IN_ACTU,1)
     From   TTINDI_ACTU   
     Where NU_ANNO = @CNNU_ANNO - 1
     And   NU_MESE = 12
  Else    
    /* obtengo indice de actualizacion a mes historico */
     Select @VNNU_IND1 = isnull(IN_ACTU,1)
     From   TTINDI_ACTU   
     Where NU_ANNO = @CNNU_ANNO
     And   NU_MESE = @CNNU_MESE
   
   If @VNNU_IND1 IS null or @VNNU_IND1 = 0.00
    Select @VNNU_IND1 = 1.00
   Select @VNFA_ACTU = @VNNU_IND2 / @VNNU_IND1
   Select @VNFA_ACTU = round(@VNFA_ACTU,3 )
   If @CNIM_CARG > 0     Begin                
     Select @VNHT_CARG = @CNIM_CARG
     Select @VNHT_ABON = 0.00
     Select @VNAT_CARG = @VNHT_CARG * @VNFA_ACTU
     Select @VNAT_ABON = 0.00
     Select @VNVA_REIN = @VNAT_CARG - @VNHT_CARG
    End
   If @CNIM_ABON > 0     Begin               
     Select @VNHT_CARG =  0.00
     Select @VNHT_ABON =  @CNIM_ABON
     Select @VNAT_CARG = 0.00
     Select @VNAT_ABON = @VNHT_ABON * @VNFA_ACTU
     Select @VNVA_REIN = @VNAT_ABON - @VNHT_ABON
     Select @VNVA_REIN = @VNVA_REIN * -1
    End
   If @ISTI_SALD =  'M' 
    Begin
     Select @VSNU_PERI = convert(char(4),@CNNU_ANNO) + "-" + SubString(convert(char(3),@CNNU_MESE+100),2,3)
   
    Insert Into #TWMVTO_CNTB_Q05_1 Values(@VSNU_PERI, @CSDE_GLOS, @VNHT_CARG, @VNHT_ABON,
     @VNFA_ACTU, @VNAT_CARG, @VNAT_ABON, @VNVA_REIN )
  End                     
 Else
  If @ISTI_SALD =  'E' And @CNIM_CARG > 0
   Begin
     Select @VSNU_PERI = convert(char(4),@CNNU_ANNO) + "-" +
      SubString(convert(char(3),@CNNU_MESE+100),2,3)
     Insert Into #TWMVTO_CNTB_Q05_1 Values(@VSNU_PERI, @CSDE_GLOS, @VNHT_CARG, @VNHT_ABON,
       @VNFA_ACTU, @VNAT_CARG, @VNAT_ABON, @VNVA_REIN )
   End
  Else If @ISTI_SALD =  'C' And  @CNIM_ABON > 0
   Begin
     Select @VSNU_PERI = convert(char(4),@CNNU_ANNO) + "-" +
       SubString(convert(char(3),@CNNU_MESE+100),2,3)
     Insert Into #TWMVTO_CNTB_Q05_1 Values(@VSNU_PERI, @CSDE_GLOS, @VNHT_CARG, @VNHT_ABON,
       @VNFA_ACTU, @VNAT_CARG, @VNAT_ABON, @VNVA_REIN )
   End
  Select @VNHT_CARG = 0.00
  Select @VNFA_ACTU = 0.00
  Select @VNAT_CARG = 0.00
  Select @VNAT_ABON = 0.00
  Select @VNVA_REIN = 0.00
  
  Fetch CU_TXMVTO_CNTB_Q05_1 Into @CNNU_ANNO, @CNNU_MESE, @CSDE_GLOS,@CNIM_CARG,@CNIM_ABON
 End
Close CU_TXMVTO_CNTB_Q05_1
Deallocate CU_TXMVTO_CNTB_Q05_1
Select NU_PERI, DE_GLOS, HT_CARG,  HT_ABON, FA_ACTU, AT_CARG, AT_ABON, VA_REI
 From #TWMVTO_CNTB_Q05_1
Order By NU_PERI
Return
/*------------------------------ Fin ---------------------------------*/















GO
